using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Dimok.Web.UI;
using Dimok.Data.DataSources;

namespace Dimok.Web.UI {

	////////////////////////////////////////////////////////////////////////////////////////////////////
	/// <summary>	User control ex. </summary>
	///
	/// <remarks>	Dimon, 12/21/2010. </remarks>
	////////////////////////////////////////////////////////////////////////////////////////////////////

	public class UserControlEx :UserControl {
		private AObjectDataSourceBase _ods;

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Gets or sets the ods. </summary>
		///
		/// <value>	The ods. </value>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		protected AObjectDataSourceBase ODS {
			get {
				try {
					if(_ods != null) return _ods;
					_ods = FindControl("ds") as AObjectDataSourceBase;
					if(_ods != null) return ODS;
					DataBoundControl dbc = null;
					if(_fv != null) dbc = FV;
					if(_gv != null) dbc = GV;
					if(dbc == null) dbc = Find.ChildByType(this,typeof(DataBoundControl)) as DataBoundControl;
					if(dbc != null)
						return _ods = Find.SiblingByID(dbc,dbc.DataSourceID) as AObjectDataSourceBase;
					return _ods = Find.ChildByType(this,typeof(AObjectDataSourceBase)) as AObjectDataSourceBase;
				} catch(Exception exc) { throw SmartException.GetControlTraceException(this,Config.UI.DebugMode,exc); }
			}
			set { _ods = value; }
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Gets the data bound container. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <returns>	The data bound container. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		private DataBoundControl GetDataBoundContainer() {
			DataBoundControl c = Find.ChildByType(this,typeof(DataBoundControl)) as DataBoundControl;
			if(typeof(FormViewEx).IsInstanceOfType(c)) _fv = c as FormViewEx;
			if(typeof(GridViewEx).IsInstanceOfType(c)) _gv = c as GridViewEx;
			return c;
		}
		private GridViewEx _gv;

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Gets the gv. </summary>
		///
		/// <value>	The gv. </value>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public GridViewEx GV { get { return (GridViewEx)GetDataBoundContainer(); } }
		private FormViewEx _fv;

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Gets the fv. </summary>
		///
		/// <value>	The fv. </value>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public FormViewEx FV { get { return (FormViewEx)GetDataBoundContainer(); } }

		private UpdatePanel _up;

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Gets the up. </summary>
		///
		/// <value>	The up. </value>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public UpdatePanel UP { get { return _up != null ? _up : _up = (UpdatePanel)Find.ChildByType(this,typeof(UpdatePanel)); } }

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Default constructor. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public UserControlEx() { }

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Raises the initialise event. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="e">	Event information to send to registered event handlers. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		protected override void OnInit(EventArgs e) {
			Page.RegisterRequiresControlState(this);
			base.OnInit(e);
			List<Control> dss = Find.ChildrenByType(this,typeof(ObjectDataSource));
			foreach (Control ds in dss)
				foreach (Parameter param in ((ObjectDataSource)ds).SelectParameters)
					if (param is ControlParameter && (((ControlParameter)param).ControlID + "") == "")
						((ControlParameter)param).ControlID = ID != null ? ID : ClientID;

		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Raises the load event. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="e">	Event information to send to registered event handlers. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		protected override void OnLoad(EventArgs e) {
			base.OnLoad(e);
			//UI.JScript.ScriptLocalIDs(Page);
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Ensures that child controls. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <exception cref="GetControlTraceException">	Thrown when getcontroltrace. </exception>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		protected override void EnsureChildControls() {
			try {
				base.EnsureChildControls();
			} catch(Exception exc) { throw SmartException.GetControlTraceException(this,Config.UI.DebugMode,exc); }
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Script local. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="sender">	Source of the event. </param>
		/// <param name="e">			Event information. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		protected void ScriptLocal(object sender, EventArgs e) { ScriptLocal(sender); }

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Script local. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="sender">	Source of the event. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		protected void ScriptLocal(object sender) {
			JS.ScriptLocalID(Page, sender as Control);
		}

		#region State Control

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Override this method to add members that need to be persisted in Control's State. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="values">	. </param>
		///
		/// <example>	values["SelectFilter"] = SelectFilter</example>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		protected virtual void GetStateValues(Hashtable values) {

		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Override this method to get members that have been  persisted in Control's State. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="values">	. </param>
		///
		/// <example>	SelectFilter = values["SelectFilter"] + ""</example>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		protected virtual void SetStateValues(Hashtable values) {
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Saves the control state. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <returns>	. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		protected override object SaveControlState() {
			Hashtable values = new Hashtable();
			GetStateValues(values);
			object state = base.SaveControlState();
			if(state != null) return new Pair(state,values);
			else return values;
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Loads a control state. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="savedState">	State of the saved. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		protected override void LoadControlState(object savedState) {
			if(savedState == null) return;
			Hashtable values = new Hashtable();
			if(savedState is Pair) {
				Pair p = savedState as Pair;
				base.LoadControlState(p.First);
				values = p.Second as Hashtable;
			} else if(savedState is Hashtable)
				values = savedState as Hashtable;
			if(values != null) SetStateValues(values);
			else base.LoadControlState(savedState);
		}
		#endregion

	}
}
